package com.fb.fbvedioplayer

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import com.fb.fbvedioplayer.CustomView.FBVideoPlayerStandard
import kotlinx.android.synthetic.main.activity_viewpager.*
import java.util.ArrayList

/**
 * <pre>
 *     author : fb
 *     e-mail : 469517834@qq.com
 *     time   : 2018/04/17
 *     desc   : viewpager中使用视频播放器
 * </pre>
 */
class ActivityViewpager :AppCompatActivity(), ViewPager.OnPageChangeListener{

    var fragmentList: MutableList<FragmentDemo> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_viewpager)
        initData()
    }

    private fun initData() {
        fragmentList.add(FragmentDemo().setIndex(0))
        fragmentList.add(FragmentDemo().setIndex(1))
        fragmentList.add(FragmentDemo().setIndex(2))
        val myAdapter = MyAdapter(supportFragmentManager)
        view_pager.setAdapter(myAdapter)
        view_pager.setOnPageChangeListener(this)
    }


    //很重要 必须写才能保证退出后视频流得到释放
    override fun onPause() {
        super.onPause()
        FBVideoPlayerStandard.releaseAllVideos()
    }
    //很重要 必须写才能保证退出后视频流得到释放
    override fun onBackPressed() {
        if (FBVideoPlayerStandard.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        FBVideoPlayerStandard.releaseAllVideos()
    }


    inner class MyAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getItem(i: Int): Fragment {
            return fragmentList[i]
        }

        override fun getCount(): Int {
            return fragmentList.size
        }

    }
}